<template>
  <div class="course-query">
      <a-tabs defaultActiveKey="1">
        <a-tab-pane tab="全部课程" key="1">
          <all-courses 
            :courses="allCourses"
            :selectedCourses="selectedCourses"
            @select="handleSelectCourse"
          />
        </a-tab-pane>
        <a-tab-pane tab="已选课程" key="2">
          <selected-courses 
            :courses="selectedCourses"
            @withdraw="handleWithdrawCourse"
          />
        </a-tab-pane>
        <a-tab-pane tab="可选课程" key="3">
          <available-courses 
            :courses="availableCourses"
            :selectedCourses="selectedCourses"
            @select="handleSelectCourse"
          />
        </a-tab-pane>
      </a-tabs>
  </div>
</template>

<script>
import AllCourses from './modules/AllCourses';
import SelectedCourses from './modules/SelectedCourses';
import AvailableCourses from './modules/AvailableCourses';

// 真实课程数据
const REAL_COURSES = [
  {
    id: 'CS101',
    name: '计算机科学导论',
    credit: 3,
    teacher: '李红梅',
    time: 'Mon-1,2',
    location: '1号教学楼',
    enrolled: 45,
    capacity: 60,
    type: 'required',
    requirePrerequisite: false
  },
  {
    id: 'MATH201',
    name: '高等数学（上）	',
    credit: 4,
    teacher: '李娜',
    time: 'Tue-3,4',
    location: '1号教学楼',
    enrolled: 50,
    capacity: 60,
    type: 'required',
    requirePrerequisite: false
  },
  {
    id: 'CS301',
    name: '数据结构与算法',
    credit: 4,
    teacher: '李红梅',
    time: 'Mon-3,4',
    location: '2号教学楼',
    enrolled: 38,
    capacity: 45,
    type: 'required',
    requirePrerequisite: true,
    prerequisite: 'CS101'
  }
];

export default {
  components: {
    AllCourses,
    SelectedCourses,
    AvailableCourses
  },
  data() {
    return {
      allCourses: [],
      selectedCourses: [],
      availableCourses: []
    };
  },
  created() {
    this.fetchAllCourses();
    this.fetchSelectedCourses();
    this.calculateAvailableCourses();
  },
  methods: {
    fetchAllCourses() {
      // 使用真实课程数据作为基础，并扩展生成更多课程
      this.allCourses = [
        ...REAL_COURSES,
        {
          id: 'CS201',
          name: '程序设计基础',
          credit: 3,
          teacher: '陈立伟',
          time: 'Tue-1,2',
          location: '2号教学楼',
          enrolled: 42,
          capacity: 50,
          type: 'required',
          requirePrerequisite: false
        },
        {
          id: 'MATH202',
          name: '线性代数',
          credit: 3,
          teacher: '张明',
          time: 'Wed-3,4',
          location: '2号教学楼',
          enrolled: 48,
          capacity: 60,
          type: 'required',
          requirePrerequisite: false
        },
        {
          id: 'CS302',
          name: '操作系统',
          credit: 4,
          teacher: '王强',
          time: 'Thu-3,4',
          location: '2号教学楼',
          enrolled: 36,
          capacity: 45,
          type: 'required',
          requirePrerequisite: true,
          prerequisite: 'CS201'
        },
        {
          id: 'CS401',
          name: '计算机网络',
          credit: 4,
          teacher: '李红梅',
          time: 'Tue-5,6',
          location: '2号教学楼',
          enrolled: 32,
          capacity: 40,
          type: 'required',
          requirePrerequisite: true,
          prerequisite: 'CS301'
        }
      ];
    },
    
    fetchSelectedCourses() {
      // 模拟已选课程，从真实课程中选取几门
      this.selectedCourses = [
        REAL_COURSES[0], // 计算机科学导论
        REAL_COURSES[1], // 高等数学（上）	
        {
          id: 'HIST101',
          name: '中国近现代史纲要',
          credit: 2,
          teacher: '刘芳',
          time: 'Wed-1,2',
          location: '文科楼201',
          enrolled: 55,
          capacity: 60,
          type: 'required',
          requirePrerequisite: false,
          status: '已确认',
          canWithdraw: true
        }
      ];
    },
    
    calculateAvailableCourses() {
      // 计算可选课程（全部课程中未选的且满足条件的）
      this.availableCourses = this.allCourses.filter(course => {
        // 过滤掉已选课程
        if (this.selectedCourses.some(sc => sc.id === course.id)) {
          return false;
        }
        
        // 这里可以添加其他可选条件，如时间不冲突、满足先修课要求等
        return true;
      });
    },
    
    handleSelectCourse(course) {
      this.selectedCourses.push({
        ...course,
        status: '已确认',
        canWithdraw: true
      });
      this.calculateAvailableCourses();
      this.$message.success(`成功选择课程: ${course.name}`);
    },
    
    handleWithdrawCourse(courseId) {
      this.selectedCourses = this.selectedCourses.filter(c => c.id !== courseId);
      this.calculateAvailableCourses();
      this.$message.success('已退选课程');
    }
  }
};
</script>

<style scoped>
.course-query {
  margin: 0;
}
</style>